<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="utf-8">
  <title>Sign in &laquo; Admin</title>
  <link rel="stylesheet" href="../assets/vendors/bootstrap/css/bootstrap.css">
  <link rel="stylesheet" href="../assets/css/admin.css">
</head>

<body>
  <!-- 登录 -->
  <div class="login">
    <div class="wrap">
      <img class="avatar" src="../assets/img/default.png">
      <!-- 有错误信息时展示 -->
      <!-- <div class="alert alert-danger">
        <strong>错误！</strong> 用户名或密码错误！
      </div> -->
      <div class="form-group">
        <label for="email" class="sr-only">邮箱</label>
        <input id="email" type="email" class="form-control" placeholder="邮箱" autofocus>
      </div>
      <div class="form-group">
        <label for="password" class="sr-only">密码</label>
        <input id="password" type="password" class="form-control" placeholder="密码">
      </div>
      <button class="btn btn-primary btn-block" id="loginBtn">登 录</button>
      <a href="javascript:;" id="goLogon">去注册</a>
    </div>
  </div>
  <!-- 注册 -->
  <div class="logon">
    <div class="wrap">
      <img class="avatar" src="../assets/img/default.png">

      <div class="form-group">
        <label for="email" class="sr-only">邮箱</label>
        <input id="email" type="email" class="form-control" placeholder="邮箱" autofocus>
      </div>
      <div class="form-group">
        <label for="password" class="sr-only">密码</label>
        <input id="password" type="password" class="form-control" placeholder="密码" name="password">
      </div>
      <div class="form-group">
        <label for="password" class="sr-only" name="repassword">再次输入密码</label>
        <input id="repassword" type="password" class="form-control" placeholder="确认密码">
      </div>
      <button class="btn btn-primary btn-block" id="logonBtn">注 册</button>
      <a href="javascript:;" id="goLogin">去登录</a>
    </div>
  </div>
  <script src="../assets/vendors/jquery/jquery.min.js"></script>
  <script type="text/javascript">
    // 点击“去注册账号”的链接
    $('#goLogon').on('click', function () {
      $('.login').hide()
      $('.logon').show()
    })

    // 点击“去登录”的链接
    $('#goLogin').on('click', function () {
      $('.login').show()
      $('.logon').hide()
    })
    // 选择登录按钮并为其添加点击事件
    $('#loginBtn').on('click', function () {
      // 获取用户输入的邮箱地址
      var email = $('#email').val();
      // 获取用户输入的密码
      var password = $('#password').val();
      // 判断用户是否输入了邮箱地址
      if (email.trim().length == 0) {
        alert('请输入邮箱')
        return;
      }
      // 判断用户是否输入了密码
      if (password.trim().length == 0) {
        alert('请输入密码');
        return;
      }

      $.ajax({
        type: 'post',
        url: '/login',
        data: {
          email: email,
          password: password
        },
        success: function (response) {
          // 登录成功 跳转到数据管理的首页面
          location.href = 'index.html';
        },
        error: function () {
          // 登录失败
          alert('用户名或者密码错误')
        }
      })

    });
    var regpwd = /^[\S]{5,12}$/;
    $('.form-group #password').on('blur', function () {
      if (regpwd.test($(this).val())) {
        var pwd = $(this).val();
        $('#repassword').on('blur', function () {
          if ($(this).val() == pwd) {
            alert('注册成功')
          }
          else {
            alert('两次密码不一致')
          }
        })
      }
      else {
        alert("输入密码格式不正确")
      }
    })
  </script>
</body>

</html>